﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using TaskManagement.ModelLayer;

namespace DataAccess
{
    public class DBTask : IDBTask
    {
        private TaskContext context;
        
        public DBTask()
        {
            context = new TaskContext();
            context.Configuration.LazyLoadingEnabled = false;
        }

        
        public IList<Task> SearchTask(Task searchTask)
        {
            IList<Task> results;
            var query = from task in context.TaskEntitySet.Include("EmployeeEntity").Include("WorkflowEntity") select task;

            if (!String.IsNullOrWhiteSpace(searchTask.Account))
                query = query.Where(task => task.Account.Equals(searchTask.Account));

            if (searchTask.Employee != null)
                query = query.Where(task => task.EmployeeEntity.Id == searchTask.Employee.Id);

            if (searchTask.Date != null)
                query = query.Where(task => task.Date == searchTask.Date);

            if (searchTask.Id != 0)
                query = query.Where(task => task.Id == searchTask.Id);

            if(!String.IsNullOrWhiteSpace(searchTask.Description))
                query = query.Where(task => task.Description.Equals(searchTask.Description));

            if (!String.IsNullOrWhiteSpace(searchTask.Delivery))
                query = query.Where(task => task.Delivery.Equals(searchTask.Delivery));

            if (searchTask.Estiamte != -1)
                query = query.Where(task => task.Estimate == searchTask.Estiamte);

            if (!String.IsNullOrWhiteSpace(searchTask.Status))
                query = query.Where(task => task.Status.Equals(searchTask.Status));

            if (!String.IsNullOrWhiteSpace(searchTask.Tag))
                query = query.Where(task => task.Tag.Equals(searchTask.Tag));

            if (searchTask.TimeSpent != -1)
                query = query.Where(task => task.TimeSpent == searchTask.TimeSpent);

            results = Converter.ConvertTasktList(query.ToList());

            return results;
        }
        
        public int CreateTask(Task task)
        {
            int ret = 0;
            try
            {
                TaskEntity te = Converter.ConvertTaskEntity(task);
                context.TaskEntitySet.Add(te);
                context.SaveChanges();
                ret += te.Id;
            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 99, EventLogEntryType.Error);
            }

            return ret;
        }

        public int DeleteTasks(IList<Task> tasks)
        {
            int ret = 0;

            try
            {
                foreach (Task task in tasks)
                {
                    
                    TaskEntity te = Converter.ConvertTaskEntity(task);
                    context.TaskEntitySet.Attach(te);
                    context.TaskEntitySet.Remove(te);
                }

                context.SaveChanges();
                ret += 1;
            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 100, EventLogEntryType.Error);
            }


            return ret;
        }


        public int UpdateTasks(Task task)
        {
            int ret = 0;
            try
            {
                TaskEntity te = Converter.ConvertTaskEntity(task);
                context.TaskEntitySet.Add(te);
                context.Entry(te).State = System.Data.EntityState.Modified;
                context.SaveChanges();
                ret = 1;
            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 101, EventLogEntryType.Error);
            }

            return ret;

        }
    }
}
